package com.epfresh.supplier.dialog;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.epfresh.api.widget.Dialog.BaseDialogView;
import com.epfresh.supplier.R;
import com.epfresh.supplier.adapter.*;
import com.epfresh.supplier.adapter.AgentFilterTitleViewBinder;
import com.epfresh.supplier.entity.BuyerFilter;

import java.util.ArrayList;
import java.util.List;

import multitype.MultiTypeAdapter;
import multitype.OnRecyclerViewItemClickListener;

/**
 * Created by wzh on 16-7-13.
 */
public class FilterCityDialog extends BaseDialogView {
    private int height;
    String defaultSelect = "城市";

    public FilterCityDialog(Context context) {
        super(context);
        setInAnim(true);
        setOutAnim(true);
        inAnim.setDuration(1);
        outAnim.setDuration(1);
    }
    public FilterCityDialog(Context context,String defaultSelect) {
        super(context);
        setInAnim(true);
        setOutAnim(true);
        inAnim.setDuration(1);
        outAnim.setDuration(1);
        this.defaultSelect = defaultSelect;
    }



    public SelectCondition selectCondition;
    public interface SelectCondition {
        void selectCity(String condition,String name);
        void selectCategory(String condition,String name);
    }

    public SelectCondition getSelectCondition() {
        return selectCondition;
    }

    public void setSelectCondition(SelectCondition selectCondition) {
        this.selectCondition = selectCondition;
    }

/*    public Animation getInAnimation() {

        return AnimationUtils.loadAnimation(this.context, com.epfresh.api.R.anim.anim_top_push_in);
    }

    public Animation getOutAnimation() {
        return AnimationUtils.loadAnimation(this.context, com.epfresh.api.R.anim.anim_top_push_out);
    }*/

    @Override
    protected void startInAnimation() {
        if (recyclerView != null) {
            recyclerView.startAnimation(inAnim);
        }
    }

    @Override
    protected void startOutAnimation() {
        if (recyclerView != null) {
            recyclerView.startAnimation(outAnim);
        }
    }

    @Override
    protected void initRes() {
        outContainerRes = R.id.f_out_container;
        containerRes = R.id.f_content_container;
        rootRes = R.layout.dialog_content_view;
        rootBgRes = R.id.f_v_content_bg;
    }

    RelativeLayout.LayoutParams lp;

    @Override
    protected void createdView() {
/*        Log.e("gird_dialog createdView", "________________");*/
        View v = LayoutInflater.from(context).inflate(R.layout.dialog_recycler_layout, contentContainer);
        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) rootView.getLayoutParams();
        lp.topMargin = height;


 /*
        //View rootView = layoutInflater.inflate(R.layout.dialog_grid_layout, contentContainer);
        View rootView = layoutInflater.inflate(R.layout.dialog_recycler_layout, null, false);
        rootView.setBackgroundColor(Color.parseColor("#00000000"));
        vContentBg.setBackgroundColor(Color.parseColor("#00000000"));
        this.rootView.setBackgroundColor(Color.parseColor("#00000000"));
        setContentView(rootView);
        View v_line_3 = rootView.findViewById(R.id.v_line_3);
        lp = (RelativeLayout.LayoutParams) v_line_3.getLayoutParams();
        lp.topMargin = height;*/
        setCancelable(true);
        initView();
    }

    RecyclerView recyclerView;
    MultiTypeAdapter mMultiAdapter;
    AgentFilterCityViewBinder titleViewBinder;
    GridLayoutManager mManager;
    List<BuyerFilter.CitiesBean> mItems = new ArrayList<>();

    private void initView() {
        recyclerView = findView(R.id.rv_dialog_filter);
        mManager = new GridLayoutManager(context, 4);
        recyclerView.setLayoutManager(mManager);
        mMultiAdapter = new MultiTypeAdapter();
        titleViewBinder = new AgentFilterCityViewBinder();
        mMultiAdapter.register(BuyerFilter.CitiesBean.class,titleViewBinder);
        recyclerView.setAdapter(mMultiAdapter);
        mMultiAdapter.setItems(mItems);
        titleViewBinder.setOnRecyclerViewItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position, Object data) {
                switch (view.getId()){
                    case R.id.tv_dialog_filter_content:
                        if(data instanceof BuyerFilter.CitiesBean)
                        {
                            if(((BuyerFilter.CitiesBean) data).isSelectFlag()){
                                ((BuyerFilter.CitiesBean) data).setSelectFlag(false);
                                selectCondition.selectCity("",defaultSelect);
                            }else{
                                for(int i=0;i<mItems.size();i++){
                                    mItems.get(i).setSelectFlag(false);
                                }
                                ((BuyerFilter.CitiesBean) data).setSelectFlag(true);
                                selectCondition.selectCity(((BuyerFilter.CitiesBean) data).getCode(),((BuyerFilter.CitiesBean) data).getName());
                            }
                            mMultiAdapter.notifyDataSetChanged();
                        }
                        break;
                }
            }
        });




        mMultiAdapter.notifyDataSetChanged();
    }
    public void show(List<BuyerFilter.CitiesBean> list, int height) {
        this.height = height;
        if (lp != null)
            lp.topMargin = height;
        mItems = list;
        show();
    }
    //测试要求，进入其他界面返回来以后要设置选中城市的状态
    public void setCitySelect(String city){
        for(int i=0;i<mItems.size();i++){
            if (mItems.get(i).getName().equals(city))
            mItems.get(i).setSelectFlag(true);
        }
        mMultiAdapter.notifyDataSetChanged();
    }
    public void show(List<BuyerFilter.CitiesBean> list, ViewGroup fatherView) {
        if(fatherView!=null){
            decorView = fatherView;
        }
        mItems = list;
        show();
    }



    @Override
    public void initBefore() {
        setGravity(Gravity.TOP);
    }

    @Override
    protected void initAnim() {
        super.initAnim();
    }


    public void setHeight(int height) {
        this.height = height;
        lp.topMargin = height;
    }
}
